package APIService;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;

import org.apache.axis.message.SOAPHeaderElement;

import auction.Auction.Service.AuctionServiceSoapProxy;
import auction.Auction.Service.AuctionServiceSoapStub;
import auction.schema.AuctionService.GetSearchResultsRequestT;
import auction.schema.AuctionService.GetSearchResultsResponseT;
import auction.schema.AuctionService.ItemTypeFilterCode;
import auction.schema.AuctionService.PaginationT;
import auction.schema.AuctionService.SearchSortOrderCode;

public class APIService {

	public GetSearchResultsResponseT GetSeachResult(String Ticket,
			 String Keyword,
			 int pageindex,SearchSortOrderCode sort)
	{
		GetSearchResultsResponseT res = new GetSearchResultsResponseT();
		try {

			// SOAP Header Save
			System.out.println("=== SOAP Header ===");
			SOAPHeaderElement soapMsgHeader = new SOAPHeaderElement(new QName("http://www.auction.co.kr/Security", "EncryptedTicket", ""));
			soapMsgHeader.setPrefix("");
			soapMsgHeader.addChildElement("Value", "", "http://www.auction.co.kr/Security").setValue(Ticket);


			AuctionServiceSoapProxy auctionProxy = new AuctionServiceSoapProxy();
			AuctionServiceSoapStub auctionStub = (AuctionServiceSoapStub) auctionProxy.getAuctionServiceSoap();

			auctionStub.setHeader(soapMsgHeader);

			GetSearchResultsRequestT req = new GetSearchResultsRequestT();

			PaginationT pagination = new PaginationT();
			pagination.setPageIndex(pageindex);
			pagination.setPageSize(20);
			req.setPagination(pagination); //paging 
			
			req.setItemType(ItemTypeFilterCode.FixedPriceExcludeStoreInventory); //Static
			
			System.out.println("Sorting "+sort.toString());
			req.setOrder(sort);
			req.setQuery(Keyword);
			res = auctionStub.getSearchResults(req);
			
			
			System.out.print("=== GetSearchResult Call End ===");
		//Exception 	
		}catch(SOAPException e){
			System.out.println(e.getMessage());
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return res;
	}
}
	